package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityCompanyDock;
import com.ocom.common.entity.company.EntityDockList;
import com.ocom.common.entity.company.EntityDockServer;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.CompanyDockComListVo;
import com.ocom.common.vo.company.CompanyDockListVo;
import com.ocom.company.dao.IDaoCompanyDock;
import com.ocom.company.service.*;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyDockServiceImpl extends ServiceImpl<IDaoCompanyDock, EntityCompanyDock> implements CompanyDockService {


    @Autowired
    IDaoCompanyDock iDaoCompanyDock;

    @Autowired
    DockListService dockListService;

    @Autowired
    DockServerService dockServerService;

    @Autowired
    DockKeyService dockKeyService;

    @Autowired
    DockUrlService dockUrlService;

    @Autowired
    ComDockApiService comDockApiService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityCompanyDock getCompanyDockInfo(String comAppid, String dockCode) {
        QueryWrapper<EntityCompanyDock> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_appid", comAppid);
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityCompanyDock getCompanyDockInfo(Long comId, String dockCode) {
        QueryWrapper<EntityCompanyDock> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<CompanyDockListVo> getComDockList(Long comId) {
        return 	iDaoCompanyDock.getComDockList(comId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Result addComDock(EntityCompanyDock request) {
        EntityCompanyDock entityCompanyDock = getCompanyDock(false, request.getComId(), request.getDockCode(), null);
        if (null != entityCompanyDock) {
            return ResultUtil.error(ResultEnum.Err_4097);
        }
        EntityDockList dockList = dockListService.getDockList(request.getDockCode());
        if (null == dockList) {
            return ResultUtil.error(ResultEnum.Err_4098);
        }
        if (StringUtils.isNotBlank(request.getServerCode())) {
            EntityDockServer dockServer = dockServerService.getDockServerInfo(request.getDockCode(), null, request.getServerCode(), null);
            if (null == dockServer) {
                return ResultUtil.error(ResultEnum.Err_4096);
            }
        }
        entityCompanyDock = getCompanyDock(false,null, request.getDockCode(), request.getComAppid());
        if (null != entityCompanyDock) {
            return ResultUtil.error(ResultEnum.Err_4095);
        }

        return this.save(request) ? ResultUtil.success() : ResultUtil.error(ResultEnum.Err_2016);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Result updateComDock(EntityCompanyDock request) {
        EntityCompanyDock entityCompanyDock = getCompanyDock(false, request.getComId(), request.getDockCode(), null);
        if (null == entityCompanyDock) {//TODO
            return ResultUtil.error(ResultEnum.Err_4099);
        }
        if (StringUtils.isNotBlank(request.getServerCode())) {
            EntityDockServer dockServer = dockServerService.getDockServerInfo(request.getDockCode(), null, request.getServerCode(), null);
            if (null == dockServer) {
                return ResultUtil.error(ResultEnum.Err_4096);
            }
        }

        EntityCompanyDock companyDockResult = getCompanyDock(true, request.getComId(), request.getDockCode(), request.getComAppid());
        if (null != companyDockResult) {
            return ResultUtil.error(ResultEnum.Err_4095);
        }
        entityCompanyDock.setServerCode(request.getServerCode());
        entityCompanyDock.setComAppid(request.getComAppid());
        entityCompanyDock.setRemark(request.getRemark());
        return this.updateById(entityCompanyDock) ? ResultUtil.success() : ResultUtil.error(ResultEnum.Err_3014);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Result removeComDock(EntityCompanyDock request) {
        EntityCompanyDock entityCompanyDock = getCompanyDock(false, request.getComId(), request.getDockCode(), null);
        if (null == entityCompanyDock) {//TODO
            return ResultUtil.error(ResultEnum.Err_4099);
        }
        removeCompanyDockByComIdAndDockCode(request.getComId(), request.getDockCode());
        dockKeyService.removeDockKeyByComIdAndDockCodeAndGrade(request.getComId(), request.getDockCode(), 0);
        dockUrlService.removeDockUrlByComIdAndDockCodeAndGrade(request.getComId(), request.getDockCode(), 0);
        comDockApiService.removeComDockApiByComIdAndDockCodeAndGrade(request.getComId(), request.getDockCode());

        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityCompanyDock getCompanyDock(Boolean isNot, Long comId, String dockCode, String comAppid) {

        QueryWrapper<EntityCompanyDock> queryWrapper = Wrappers.query();
        if (null != comId) {
            if (isNot) {
                queryWrapper.ne("com_id", comId);
            } else {
                queryWrapper.eq("com_id", comId);
            }
        }
        if (StringUtils.isNotBlank(dockCode)) {
            queryWrapper.eq("dock_code", dockCode);
        }
        if (StringUtils.isNotBlank(comAppid)) {
            queryWrapper.eq("com_appid", comAppid);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public boolean removeCompanyDockByComIdAndDockCode(Long comId, String dockCode) {
        QueryWrapper<EntityCompanyDock> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("dock_code", dockCode);
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<CompanyDockComListVo> getDockComList(String dockCode, String serverCode) {
        return 	iDaoCompanyDock.getDockComList(dockCode, serverCode);
    }
}
